<template>
  <div class="attachment__module">
    <transition name="images-perview">
      <div class="images-perview__group" v-if="imagesDetailShow">
        <div :key="index" class="image-info__group" v-for="(image,index) in renderImages">
          <i
            @click="deleteHandelr(image,index)"
            class="delete-btn__item el-icon-delete"
            title="删除"
          />
          <el-image :src="image.path|resourceUrl" class="image__item" fit="cover" />
          <div class="rid__item">rid: {{image.rid}}</div>
          <el-link class="images-load__btn">
            <i class="el-icon-document-copy" /> 复制连接
          </el-link>
        </div>
      </div>
    </transition>
    <div class="images-count__item">
      共包含图片资源 ({{imagesCount}})
      <el-link @click="checkDetailHandler" class="images-load__btn" type="primary">{{showBtnText}}</el-link>
    </div>
  </div>
</template>

<script>
export default {
    name: 'ArticleAttachment',
    props: {
        images: {
            type: Array,
            default: () => [],
        },
    },
    data() {
        return {
            imagesDetailShow: false,
            imagesDetail: [],
        };
    },
    computed: {
        showBtnText() {
            return this.imagesDetailShow ? '收起' : '展开';
        },
        resetImages() {
            return this.images.map((v) => {
                this.$set(v, 'isDelete', false);
                return v;
            });
        },
        renderImages() {
            return this.resetImages.filter((v) => !v.isDelete);
        },
        imagesCount() {
            return this.renderImages.length;
        },
    },
    methods: {
        checkDetailHandler() {
            this.imagesDetailShow = !this.imagesDetailShow;
        },
        async deleteHandelr(image) {
            // 删除操作, 1.在显示上的删除, 2.在数据库上的删除
            image.isDelete = true; // 1.在显示上的删除
            const arr = this.renderImages.reduce((pv, v) => {
                pv.push(v.rid);
                return pv;
            }, []);
            this.$emit('delete', { value: arr.toString(), imageTarget: image }); // 2. 在稿件附件表中的删除,在资源表中的删除
        },
    },
};
</script>

<style lang="scss">
.attachment__module {
  box-sizing: border-box;
  font-size: 12px;
  color: #666;
  .images-count__item {
    margin-bottom: 30px;
  }
  .images-load__btn {
    font-size: 12px;
  }
  $image-perview-height: 170px;
  .images-perview__group {
    height: $image-perview-height;
    overflow: hidden;
    overflow-x: auto;
    display: flex;
    justify-content: flex-start;
    transition: all 0.3s;
    .image-info__group {
      height: 128px;
      position: relative;
      margin-right: 10px;
      display: flex;
      flex-direction: column;
      justify-content: center;
      flex-shrink: 0;
    }

    .delete-btn__item {
      padding: 5px;
      position: absolute;
      top: 0;
      right: 0;
      z-index: 9;
      cursor: pointer;
      border-radius: 0 0 0 4px;
      background-color: rgba($color: #000000, $alpha: 0.3);
      color: #fff;
      flex-shrink: 0;
    }
    .delete-btn__item:hover {
      background-color: rgba($color: #000000, $alpha: 0.6);
    }
    .rid__item {
      line-height: 1.8;
      position: absolute;
      top: 0;
      left: 0;
      font-size: 12px;
      transform: scale(0.8);
      text-shadow: 1px 1px #fff;
      color: #aaa;
    }
    .image__item {
      width: 100px;
      height: 100px;
      flex-shrink: 0;
      border-radius: 4px;
    }
  }

  .images-perview-enter-to {
    height: $image-perview-height;
  }
  .images-perview-enter,
  .images-perview-leave-to {
    height: 0px;
  }
}
</style>
